// Decompiled by DJ v3.9.9.91 Copyright 2005 Atanas Neshkov  Date: 4/12/2007 10:47:37
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   Quaternion.java

package mathematics;


// Referenced classes of package de.escape.quincunx.trafo:
//            Matrix4D, Vector3D

public final class Quaternion
{

    public Quaternion(Vector3D from, Vector3D to)
    {
        from.normalize();
        to.normalize();
        x = from.y * to.z - from.z * to.y;
        y = from.z * to.x - from.x * to.z;
        z = from.x * to.y - from.y * to.x;
        w = from.x * to.x + from.y * to.y + from.z * to.z;
    }

    public Quaternion(Quaternion quat)
    {
        this(quat.x, quat.y, quat.z, quat.w);
    }

    public Quaternion(float x, float y, float z, float w)
    {
        this.x = x;
        this.y = y;
        this.z = z;
        this.w = w;
    }

    public Quaternion()
    {
        this(0.0F, 0.0F, 0.0F, 1.0F);
    }

    public Matrix4D getMatrix()
    {
        float norm = x * x + y * y + z * z + w * w;
        float scale = norm <= 0.0F ? 0.0F : 2.0F / norm;
        float xs = x * scale;
        float ys = y * scale;
        float zs = z * scale;
        float wx = w * xs;
        float wy = w * ys;
        float wz = w * zs;
        float xx = x * xs;
        float xy = x * ys;
        float xz = x * zs;
        float yy = y * ys;
        float yz = y * zs;
        float zz = z * zs;
        return new Matrix4D(1.0F - yy - zz, xy - wz, xz + wy, 0.0F, xy + wz, 1.0F - xx - zz, yz - wx, 0.0F, xz - wy, yz + wx, 1.0F - xx - yy, 0.0F, 0.0F, 0.0F, 0.0F, 1.0F);
    }

    public Quaternion mult(Quaternion quat)
    {
        return new Quaternion((w * quat.x + x * quat.w + y * quat.z) - z * quat.y, (w * quat.y + y * quat.w + z * quat.x) - x * quat.z, (w * quat.z + z * quat.w + x * quat.y) - y * quat.x, w * quat.w - x * quat.x - y * quat.y - z * quat.z);
    }

    public void multBy(Quaternion quat)
    {
        float nw = w * quat.w - x * quat.x - y * quat.y - z * quat.z;
        float nx = (w * quat.x + x * quat.w + y * quat.z) - z * quat.y;
        float ny = (w * quat.y + y * quat.w + z * quat.x) - x * quat.z;
        float nz = (w * quat.z + z * quat.w + x * quat.y) - y * quat.x;
        w = nw;
        x = nx;
        y = ny;
        z = nz;
    }

    public String toString()
    {
        return "[#" + x + "," + y + "," + z + "," + w + "#]";
    }

    private float x;
    private float y;
    private float z;
    private float w;
}